/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Window.java
 *
 * Created on 2010-12-13, 11:05:17
 */

package alggen;

import java.awt.Color;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Macias
 */
public class Window extends javax.swing.JFrame {
    Image obrazek;
    MyCanvas canvas;
    MyCanvas2 wynikiCanvas;
    Algorytm algorytm;
    Wyniki wyniki;
    WynikiWindow wynikiWindow;

    Thread algorytmThread;
    /** Creates new form Window */
    public Window() {
        initComponents();
        canvas = new MyCanvas();
        canvas.setSize(512,512);
        canvasPanel.add(canvas);
        this.setVisible(true);
        wyniki = new Wyniki();
        wynikiCanvas = new MyCanvas2(wyniki);
        algorytm = new Algorytm(wyniki);
        canvas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                Window.this.mousePressed(evt);
            }
        });


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        canvasPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        startPLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        endPLabel = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        wielkoscPopulacjiLabel = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        maxPopulationCountLabel = new javax.swing.JLabel();
        jSlider2 = new javax.swing.JSlider();
        jSeparator5 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        populacjiNaSkokLabel = new javax.swing.JLabel();
        jSlider3 = new javax.swing.JSlider();
        jSeparator6 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        kosztLabel = new javax.swing.JLabel();
        postep = new javax.swing.JProgressBar();
        jButtonZakoncz = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        iloscPunktowPosLabel = new javax.swing.JLabel();
        sIloscPunktowPos = new javax.swing.JSlider();
        jSeparator7 = new javax.swing.JSeparator();
        jLabel9 = new javax.swing.JLabel();
        tfG0 = new javax.swing.JTextField();
        cbUseR2 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Algorytm grawitacyjny - poszukiwanie ścieżki robota");
        setResizable(false);

        jButton1.setText("Wczytaj Mapę");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        canvasPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        canvasPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                Window.this.mousePressed(evt);
            }
        });

        javax.swing.GroupLayout canvasPanelLayout = new javax.swing.GroupLayout(canvasPanel);
        canvasPanel.setLayout(canvasPanelLayout);
        canvasPanelLayout.setHorizontalGroup(
            canvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 512, Short.MAX_VALUE)
        );
        canvasPanelLayout.setVerticalGroup(
            canvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 512, Short.MAX_VALUE)
        );

        jLabel1.setText("Punkt Początkowy:");

        startPLabel.setText("0 , 0 ");

        jLabel2.setText("Punkt Końcowy:");

        endPLabel.setText("255 , 255");

        jButton2.setText("Start");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Ilość cząstek:");

        jSlider1.setMaximum(500);
        jSlider1.setMinimum(1);
        jSlider1.setMinorTickSpacing(10);
        jSlider1.setPaintTicks(true);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        wielkoscPopulacjiLabel.setText("50");

        jLabel4.setText("Ilość iteracji:");

        maxPopulationCountLabel.setText("100");

        jSlider2.setMaximum(2000);
        jSlider2.setMinorTickSpacing(100);
        jSlider2.setPaintTicks(true);
        jSlider2.setSnapToTicks(true);
        jSlider2.setValue(100);
        jSlider2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider2StateChanged(evt);
            }
        });

        jLabel5.setText("Odswież co");

        populacjiNaSkokLabel.setText("10");

        jSlider3.setMaximum(1000);
        jSlider3.setMinimum(1);
        jSlider3.setMinorTickSpacing(10);
        jSlider3.setPaintTicks(true);
        jSlider3.setValue(10);
        jSlider3.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider3StateChanged(evt);
            }
        });

        jLabel6.setText("iteracjii.");

        jLabel7.setText("Najlepszy wynik:");

        kosztLabel.setText("brak");

        postep.setForeground(new java.awt.Color(0, 255, 0));

        jButtonZakoncz.setText("Stop");
        jButtonZakoncz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZakonczActionPerformed(evt);
            }
        });

        jButton3.setText("Pokaż wykres");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel8.setText("Ilość punktów pośrednich:");

        iloscPunktowPosLabel.setText("5");

        sIloscPunktowPos.setMaximum(10);
        sIloscPunktowPos.setMinimum(1);
        sIloscPunktowPos.setMinorTickSpacing(1);
        sIloscPunktowPos.setPaintTicks(true);
        sIloscPunktowPos.setSnapToTicks(true);
        sIloscPunktowPos.setValue(5);
        sIloscPunktowPos.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sIloscPunktowPosStateChanged(evt);
            }
        });

        jLabel9.setText("G0 = ");

        tfG0.setText("1.0");
        tfG0.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfG0FocusLost(evt);
            }
        });

        cbUseR2.setText("używaj r^2 zamiast r");
        cbUseR2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbUseR2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(canvasPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(startPLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonZakoncz))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(endPLabel))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(wielkoscPopulacjiLabel)
                                        .addGap(34, 34, 34))
                                    .addComponent(jSeparator5, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(maxPopulationCountLabel))
                                    .addComponent(jSeparator6, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(populacjiNaSkokLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel6))
                                    .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .addComponent(jSlider2, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                    .addComponent(jSlider3, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(iloscPunktowPosLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator7, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                            .addComponent(sIloscPunktowPos, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbUseR2))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(kosztLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(postep, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfG0, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(canvasPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(12, 12, 12)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(startPLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(endPLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(wielkoscPopulacjiLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(maxPopulationCountLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(populacjiNaSkokLabel)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(iloscPunktowPosLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sIloscPunktowPos, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(tfG0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cbUseR2)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonZakoncz)
                            .addComponent(jButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(postep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(kosztLabel)
                            .addComponent(jButton3))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
        if(fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
            File file = fc.getSelectedFile();
            System.out.println(file.getPath());
            obrazek = Toolkit.getDefaultToolkit().getImage(file.getPath());
            obrazek = obrazek.getScaledInstance(256, 256, Image.SCALE_SMOOTH);
            while(obrazek.getWidth(null)<=0)
                try {
                    //System.out.println("czekam");
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
                }
//            System.out.println(obrazek.getWidth(null)+",  "+obrazek.getHeight(null));
            canvas.obrazek = obrazek;
            canvas.invalidate();
            canvas.repaint();

            //odczytanie mapy wysokosci wedlug jasnosci pikseli
            int w = obrazek.getWidth(null);
            int h = obrazek.getHeight(null);
            int [] pixels = new int[w*h];
            PixelGrabber pg = new PixelGrabber(obrazek, 0,0, w,h, pixels, 0, w);
            try{
                pg.grabPixels();
            } catch(Exception e){};

            System.out.println("grabbing...");
                try{
                    Thread.sleep(5);
                } catch(Exception e){};
          //czekamy az zlapie piksele
            while((pg.getStatus() & ImageObserver.FRAMEBITS)!=0)
                try{
                    Thread.sleep(5);
                } catch(Exception e){}


                System.out.println("aborting...");
             pg.abortGrabbing();
            int[][] heights = new int[w][h];
             Color color;
             float[] hsb = new float[3];
             System.out.println("creating map...");
            for(int i=0;i<w;i++)
                for(int j=0; j<h; j++){
                    color = new Color(pixels[j*w+i]);
                    color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
                   heights[i][j] =(int)(240- hsb[2]*240);
                }
                System.out.println("map created!");
                algorytm.mapa=heights;
                jButton2.setEnabled(true);

         }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void mousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mousePressed
        // TODO add your handling code here:
        int ktoryPrzycisk = evt.getButton();

        if(ktoryPrzycisk == evt.BUTTON1){
            canvas.startPoz= new Point( (int) (evt.getX() * 0.5), (int) (evt.getY() * 0.5));
            algorytm.startPoz= new Point( (int) (evt.getX() * 0.5), (int) (evt.getY() * 0.5));
            startPLabel.setText( (int) (evt.getX() * 0.5)+" , "+ (int) (evt.getY() * 0.5));
        }
        if(ktoryPrzycisk == evt.BUTTON3){
            canvas.endPoz= new Point( (int) (evt.getX() * 0.5), (int) (evt.getY() * 0.5));
            algorytm.endPoz= new Point( (int) (evt.getX() * 0.5), (int) (evt.getY() * 0.5));
            endPLabel.setText( (int) (evt.getX() * 0.5)+" , "+ (int) (evt.getY() * 0.5));
        }
        canvas.invalidate();
        canvas.repaint();
//        System.out.println(evt.getX() + " "+ evt.getY());
        
    }//GEN-LAST:event_mousePressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int n = jSlider2.getValue();
        float f= (float)n/(float)jSlider3.getValue();

        //###################obliczac rozmiar tablicy wynikow
        if(Math.round(f)-f==0)
            wyniki.setSize(n);
        else
            wyniki.setSize(((int)f+1)*jSlider3.getValue());
        algorytmThread = new Thread(new Runnable(){
            public void run(){
                jButton2.setEnabled(false);
                jSlider1.setEnabled(false);
                jSlider2.setEnabled(false);
                jSlider3.setEnabled(false);
                sIloscPunktowPos.setEnabled(false);
                tfG0.setEnabled(false);
                cbUseR2.setEnabled(false);
                postep.setValue(0);
                algorytm.inicjalizuj();
                while(algorytm.pokolenieID<algorytm.maxPokolenieCount && !algorytm.stopAlgorytm){
                    canvas.osobnik = algorytm.obliczKrok();
                    canvas.osobniki = algorytm.osobniki;
                    kosztLabel.setText(""+algorytm.najlepszyOsobnik.wartPrzyst);
                    canvas.invalidate();
                    canvas.repaint();
                    wynikiCanvas.invalidate();
                    wynikiCanvas.repaint();
                    
//                    JOptionPane.showMessageDialog(null, "Continue?");

                    postep.setValue(algorytm.pokolenieID);

                    System.out.println("czekam");
                    try {
                        Thread.sleep(4);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                jButton2.setEnabled(true);
                jSlider1.setEnabled(true);
                jSlider2.setEnabled(true);
                jSlider3.setEnabled(true);
                sIloscPunktowPos.setEnabled(true);
                tfG0.setEnabled(true);
                cbUseR2.setEnabled(true);
            }
        });
        algorytmThread.start();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
        // TODO add your handling code here:
        algorytm.wielkoscPopulacji = jSlider1.getValue();
        wielkoscPopulacjiLabel.setText(jSlider1.getValue()+"");
    }//GEN-LAST:event_jSlider1StateChanged

    private void jSlider2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider2StateChanged
        // TODO add your handling code here:
        algorytm.maxPokolenieCount = jSlider2.getValue();
        maxPopulationCountLabel.setText(jSlider2.getValue()+"");
        postep.setMaximum(jSlider2.getValue());
    }//GEN-LAST:event_jSlider2StateChanged

    private void jSlider3StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider3StateChanged
        // TODO add your handling code here:
        algorytm.populacjiNaSkok = jSlider3.getValue();
        populacjiNaSkokLabel.setText(jSlider3.getValue()+"");
    }//GEN-LAST:event_jSlider3StateChanged

    private void jButtonZakonczActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZakonczActionPerformed
        algorytm.stopAlgorytm = true;
    }//GEN-LAST:event_jButtonZakonczActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if(wynikiWindow!=null && wynikiWindow.live)
            wynikiWindow.dispose();
        wynikiWindow = new WynikiWindow(wynikiCanvas);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void tfG0FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfG0FocusLost
        float G0 = 0;

        G0 = Float.parseFloat(tfG0.getText());

        if(G0 <= 0)
            G0 = 1f;

        tfG0.setText(""+G0);
        algorytm.G0 = G0;

        System.out.println("G0 changed to: " + algorytm.G0);
    }//GEN-LAST:event_tfG0FocusLost

    private void cbUseR2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbUseR2ActionPerformed
        algorytm.useR2 = cbUseR2.isSelected();

        System.out.println("use r^2: " + algorytm.useR2);
    }//GEN-LAST:event_cbUseR2ActionPerformed

    private void sIloscPunktowPosStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sIloscPunktowPosStateChanged
        algorytm.iloscPunktowPos = sIloscPunktowPos.getValue();
        iloscPunktowPosLabel.setText("" + algorytm.iloscPunktowPos);

        System.out.println("Ilosc punktow posrednich changed to: " + algorytm.iloscPunktowPos);
    }//GEN-LAST:event_sIloscPunktowPosStateChanged

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new Window().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel canvasPanel;
    private javax.swing.JCheckBox cbUseR2;
    private javax.swing.JLabel endPLabel;
    private javax.swing.JLabel iloscPunktowPosLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButtonZakoncz;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    private javax.swing.JSlider jSlider3;
    private javax.swing.JLabel kosztLabel;
    private javax.swing.JLabel maxPopulationCountLabel;
    private javax.swing.JLabel populacjiNaSkokLabel;
    private javax.swing.JProgressBar postep;
    private javax.swing.JSlider sIloscPunktowPos;
    private javax.swing.JLabel startPLabel;
    private javax.swing.JTextField tfG0;
    private javax.swing.JLabel wielkoscPopulacjiLabel;
    // End of variables declaration//GEN-END:variables

}
